package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.SmsSpikeNotification;
import com.mall.service.SmsSpikeNotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "秒杀活动与通知关系管理")
@RestController
@RequestMapping("/spikenotifications")
public class SmsSpikeNotificationController {

    @Resource
    private SmsSpikeNotificationService smsSpikeNotificationService;

    @ApiOperation(value = "新增秒杀活动与通知关系")
    @PostMapping
    public Result save(@RequestBody SmsSpikeNotification smsSpikeNotification) {
        boolean flag = smsSpikeNotificationService.save(smsSpikeNotification);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }


    @ApiOperation(value = "删除秒杀活动与通知关系")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsSpikeNotificationService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "查询秒杀活动与通知关系")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsSpikeNotification smsSpikeNotification = new SmsSpikeNotification();
        smsSpikeNotification = smsSpikeNotificationService.getById(id);
        Integer code = smsSpikeNotification !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsSpikeNotification !=null ? "successful":"failed,please try again";
        return new Result(smsSpikeNotification,code,msg);
    }

    @ApiOperation(value = "查询全部秒杀活动与通知关系")
    @GetMapping
    public Result getAll(){
        List<SmsSpikeNotification> list = smsSpikeNotificationService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsSpikeNotificationService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsSpikeNotification> getPage(@RequestParam Integer pageNum,
                                               @RequestParam Integer pageSize,
                                               @RequestParam String title) {
        IPage<SmsSpikeNotification> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsSpikeNotification> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return smsSpikeNotificationService.page(page,queryWrapper);
    }
}
